Learning Swift by Robin Nixon

Learning Swift by Robin Nixon

Author:Robin Nixon
Language: eng
Format: epub
Publisher: O'Reilly Media, Inc.
Published: 2017-03-27T04:00:00+00:00


that are not url. We use the original URL because if urlForModifying is different

from the original url variable, we may not actually remove the entry from the list.

Lastly, we update the list of files by calling the collection view’s reloadData method.

Run the app, and tap the Edit button. The delete buttons will appear, and you can tap

them to delete them.

Renaming Documents

Finally, we’ll add the ability to rename documents when you tap their labels. The code

for this will work in a similar way to deleting them: we’ll give each cell a closure to

run when the user taps the label; and in this closure, we’ll present a box that lets the

user enter a new name.

To detect taps on the label, we need to create a gesture recognizer and connect it. We’ll

be using a very simple “tap” gesture recognizer in this chapter, but we’ll be using a

more complex one later, in “Deleting Attachments” on page 310:

1. Open Main.storyboard, and locate the label in the collection view cell.

2. Select the label, and go to the Attributes Inspector. Scroll down to the View sec‐

tion in the inspector, and select the User Interaction Enabled checkbox. This will

allow the label to respond to taps.

Let’s now add the ability to detect when the user has tapped the label:

1. Add the following code to the collectionView(_, cellForItemAt: indexPath)

method, after the deletion code:

override func collectionView(_ collectionView: UICollectionView,

cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

// Get our cell

let cell = collectionView

.dequeueReusableCell(withReuseIdentifier: "FileCell",

for: indexPath) as! FileCollectionViewCell

// Get this object from the list of known files

let url = availableFiles[indexPath.row]

// Get the display name

var fileName : AnyObject?

do {

try (url as NSURL).getResourceValue(&fileName,

forKey: URLResourceKey.nameKey)

if let fileName = fileName as? String {

cell.fileNameLabel!.text = fileName

Renaming Documents | 247

}

} catch {

cell.fileNameLabel!.text = "Loading..."

}

cell.setEditing(self.isEditing, animated: false)

cell.deletionHander = {

self.deleteDocumentAtURL(url)

}

> let labelTapRecognizer = UITapGestureRecognizer(target:cell,

> action: #selector(FileCollectionViewCell.renameTapped))))

>

> cell.fileNameLabel?.gestureRecognizers = [labelTapRecognizer]

>

> cell.renameHander = {

> self.renameDocumentAtURL(url)

> }

// If this cell is openable, make it fully visible, and

// make the cell able to be touched

if itemIsOpenable(url) {

cell.alpha = 1.0

cell.isUserInteractionEnabled = true

} else {

// But if it's not, make it semitransparent, and

// make the cell not respond to input

cell.alpha = 0.5

cell.isUserInteractionEnabled = false

}

return cell

}

This code does several things:

• First, it removes any existing gesture recognizers from the label. This is neces‐

sary because cells get reused; if we don’t remove existing recognizers, we’ll end

up with labels that attempt to rename multiple files at once when they’re

tapped.

• Next, it creates a new UITapGestureRecognizer and makes it call the cell’s

renameTapped method. It then adds it to the label. Once this is done, tapping

the label will make the cell call the rename handler block, which is added next.

The rename block simply calls the renameDocumentAtURL method, which you’ll

add in a second.

2. Add the following method to DocumentListViewController:

248 | Chapter 8: Working with Files in iCloud

func renameDocumentAtURL(_ url: URL) {

// Create an alert box

let renameBox = UIAlertController(title: "Rename Document",

message: nil, preferredStyle: .alert)

// Add a text field to it that contains its current name, sans ".note"

renameBox.addTextField(configurationHandler: { (textField) -> Void in

let filename = url.lastPathComponent

.replacingOccurrences(of: ".note", with: "")

textField.text = filename

})

// Add the cancel button, which does nothing

renameBox.addAction(UIAlertAction(title: "Cancel",

style: .



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(73931)
What's Done in Darkness by Kayla Perrin(26963)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20861)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20622)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20540)
The Fifty Shades Trilogy & Grey by E L James(19469)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19386)
Shot Through the Heart by Mercy Celeste(19244)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17391)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17363)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17193)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(17103)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16716)
The Subtle Art of Not Giving a F*ck by Mark Manson(14842)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14443)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(14232)
Cozy crochet hats: 7 Stylish and Beginner-Friendly Patterns from Baby Beanies to Trendy Bucket Hats by Vanilla Lazy(13511)
Scorched Earth by Nick Kyme(13106)
Reichel W. Numerical methods for Electrical Engineering, Meteorology,...2022 by Unknown(12980)
Drei Generationen auf dem Jakobsweg by Stein Pia(11268)